//
//  RegisterVerifyCodeViewController.m
//  XDBaseFrameWork
//
//  Created by xindao on 14-9-28.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "RegisterVerifyCodeViewController.h"
#import "RegisterFinishUserInfoViewController.h"

@interface RegisterVerifyCodeViewController ()<UITextFieldDelegate>
{
    UIButton  *finishButton;
    UITextField  *verifyCodeField;
    UITextField *newPassWord;
    UIButton *comfirmButton;
    UIScrollView * mScrollView;
}
@end

@implementation RegisterVerifyCodeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.titleLabel.text = @"注册";
     self.contentView.backgroundColor = UIColorFromRGB(0xffffefa);
    
    mScrollView = [[UIScrollView alloc] initWithFrame:self.contentView.bounds];
   
    mScrollView.contentSize = CGSizeMake(UI_SCREEN_WIDTH, self.contentView.frame.size.height);
    [self.contentView addSubview:mScrollView];
    
    finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    finishButton.frame =  CGRectMake(10, 40-24-2,29,24);
//    [finishButton setTitle:@"完成" forState:UIControlStateNormal];
    [finishButton setBackgroundImage:LOADIMAGE(@"shouqi@2x") forState:UIControlStateNormal];
    [finishButton addTarget:self action:@selector(finishButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIView *assoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 40)];
    assoryView.backgroundColor = [UIColor clearColor];
    [assoryView addSubview:finishButton];
    
    
    verifyCodeField = [[UITextField alloc]initWithFrame:CGRectMake(20, 30,(UI_SCREEN_WIDTH - 20*2), 40)];
    verifyCodeField.placeholder = @"请输入验证码";
    verifyCodeField.textAlignment = NSTextAlignmentLeft;
    verifyCodeField.tag = 333;
    verifyCodeField.clearButtonMode = UITextFieldViewModeWhileEditing;
    verifyCodeField.keyboardType = UIKeyboardTypeNumberPad;
    verifyCodeField.delegate = self;
    verifyCodeField.borderStyle = UITextBorderStyleRoundedRect;
    verifyCodeField.leftView.hidden = NO;
    verifyCodeField.hidden = NO;
    verifyCodeField.inputAccessoryView = assoryView;
    verifyCodeField.leftViewMode  = UITextFieldViewModeAlways;
    [mScrollView addSubview:verifyCodeField];
    
    newPassWord = [[UITextField alloc]initWithFrame:CGRectMake(20,height_y(verifyCodeField)+20,(UI_SCREEN_WIDTH - 20*2), 40)];
    newPassWord.hidden = NO;
    newPassWord.placeholder = @"请输入密码";
    newPassWord.textAlignment = NSTextAlignmentLeft;
    newPassWord.secureTextEntry = YES;
    newPassWord.tag = 555;
    newPassWord.delegate = self;
    newPassWord.clearButtonMode = UITextFieldViewModeWhileEditing;
    newPassWord.borderStyle = UITextBorderStyleRoundedRect;
    newPassWord.leftView.hidden = NO;
    newPassWord.leftViewMode  = UITextFieldViewModeAlways;
    newPassWord.inputAccessoryView = assoryView;
    newPassWord.returnKeyType  =  UIReturnKeyGo;
    [mScrollView addSubview:newPassWord];
    
    comfirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    comfirmButton.frame = CGRectMake(15, height_y(newPassWord)+20, (UI_SCREEN_WIDTH - 2*15), 40);
    [comfirmButton setBackgroundImage:LOADIMAGE(@"pressedBg@2x") forState:UIControlStateNormal];
    [comfirmButton setTitle:@"下一步" forState:UIControlStateNormal];
    [comfirmButton addTarget:self action:@selector(comfirmButtonPressedToNextVC) forControlEvents:UIControlEventTouchUpInside];
     [comfirmButton setBackgroundImage:[UIImage imageNamed:@"pressedBg"] forState:UIControlStateHighlighted];
    comfirmButton.titleLabel.font = [UIFont systemFontOfSize:17.0f];
    [mScrollView addSubview:comfirmButton];
    
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == newPassWord){
        if (iPhone5){
        
        }else{
            mScrollView.contentSize = CGSizeMake(0, self.contentView.frame.size.height+40);
            mScrollView.contentOffset = CGPointMake(0, 40);
        }
        
    }else{
        if (iPhone5){
            
        }else{
            mScrollView.contentSize = CGSizeMake(0, self.contentView.frame.size.height+40);
            mScrollView.contentOffset = CGPointMake(0,0);
        }
    }
    return YES;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)finishButtonPressed
{
    [verifyCodeField resignFirstResponder];
    [newPassWord resignFirstResponder];
    
    if (iPhone5){
        
    }else{
        mScrollView.contentSize = CGSizeMake(0, self.contentView.frame.size.height);
        mScrollView.contentOffset = CGPointMake(0,0);
    }
}
-(void)comfirmButtonPressedToNextVC
{
    if ([verifyCodeField.text isEqualToString:[NSString stringWithFormat:@"%@",_verifyCode]]) {
        if (newPassWord.text.length >= 6 && newPassWord.text.length <= 10) {
            RegisterFinishUserInfoViewController *userinfo = [[RegisterFinishUserInfoViewController alloc]init];
            userinfo.verifyCode = _verifyCode;
            userinfo.phoneNumber = _phoneNumber;
            userinfo.passwordString = newPassWord.text;
            [self.navigationController pushViewController:userinfo animated:YES];
        }else{
           [XDTools showTips:@"密码需6-10位" toView:self.contentView];
        }
    }else{
        [XDTools showTips:@"验证码错误❌" toView:self.contentView];
    }
}


-(void)postRegisterMessage
{

    //http://123.57.16.119/index.php?m=Api&c=Customer&a=getCustomerOrder&CustomerID=''
    //http://123.57.16.119/index.php?m=Api&c=Index&a=sendsms

    if(![XDTools NetworkReachable]){
        [XDTools showTips:brokenNetwork toView:self.contentView];
        return;
    }
    
    NSDictionary *requestDic = @{@"MobilePhone":@"13301382017",};
    ASIHTTPRequest *request = [XDTools postRequestWithDict:requestDic API:API_SMSGET];
    __weak ASIHTTPRequest * mrequest = request;
    [request setCompletionBlock:^{
        NSDictionary *resultDic = [XDTools JSonFromString:[mrequest responseString]];
        DDLOG(@"res....%@",resultDic);
        if ([[resultDic objectForKey:@"status"] intValue]==1) {
            //                [XDTools showTips:[requestDic objectForKey:@"msg"] toView:self.contentView];


        }else{
            [XDTools showErrorTips:[resultDic objectForKey:@"msg"] toView:self.view];
        }

    }];
    [request setFailedBlock:^{
        [XDTools hideProgress:self.contentView];
        if (mrequest.error.code == 2) {
            [XDTools showTips:@"网络请求超时" toView:self.view];
        }
    }];
    [request setTimeOutSeconds:12];
    [request startAsynchronous];

}

@end
